package com.lhkj.ct.meta.modules.report.word;

import com.lhkj.ct.meta.modules.gauge.common.enums.QuestionType;
import com.lhkj.ct.meta.modules.questionnaire.model.vo.QnrResultVo;
import com.lhkj.ct.meta.modules.report.AbstractReportHandle;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;

/**
 * <p>
 *     问卷作答结果导出处理类
 * </p>
 */
public class QuestionnaireResultsHandle extends AbstractReportHandle<QnrResultVo, XWPFDocument> {

    public QuestionnaireResultsHandle() {
    }

    @Override
    public XWPFDocument generateReport(QnrResultVo data) {
        XWPFDocument document = new XWPFDocument();
        // step 1.标题
        XWPFParagraph titlePara = document.createParagraph();
        titlePara.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titlePara.createRun();
        titleRun.setFontSize(17);
        titleRun.setFontFamily("宋体");
        titleRun.setBold(true);
        titleRun.setText(data.getQnrName());
        // step 2.指导语
        XWPFParagraph guidancePara = document.createParagraph();
        XWPFRun guidanceRun = guidancePara.createRun();
        guidanceRun.setFontSize(15);
        guidanceRun.setFontFamily("宋体");
        guidanceRun.setBold(true);
        guidanceRun.setText("问卷简介：");
        XWPFRun guidanceParaRun = guidancePara.createRun();
        guidanceParaRun.setFontSize(13);
        guidanceParaRun.setFontFamily("宋体");
        guidanceParaRun.setText(data.getIntroduction());
        // step 3.题目
        for (QnrResultVo.Question question : data.getQuestions()) {
            XWPFParagraph questionPara = document.createParagraph(); // 创建段落
            // step 题目内容
            XWPFRun questionRun = questionPara.createRun(); // 创建段落文本
            questionRun.getCTR().setRPr(guidanceParaRun.getCTR().getRPr()); // 复制样式
            questionRun.setText(question.getQutNum() + "." + question.getQutContent());  // 设置文本内容
            // step 填空
            if (question.getType() == QuestionType.text) {
                questionRun.setText(question.getAnswer());
                XWPFRun answerRun = questionPara.createRun();
                answerRun.setUnderline(UnderlinePatterns.SINGLE);
                answerRun.setUnderlineColor("7CB5EC");
                answerRun.setText(question.getAnswer());
                answerRun.setColor("7CB5EC");
                continue;
            }
            // step 简答
            if (question.getType() == QuestionType.textarea) {
                questionRun.addBreak();
                XWPFRun textarea = questionPara.createRun();
                textarea.setText(question.getAnswer());
                textarea.setColor("7CB5EC");
                continue;
            }
            // step 单选多选
            if (question.getType() == QuestionType.radio || question.getType() == QuestionType.checkbox) {
                for (QnrResultVo.Question.Option option : question.getOptions()) {
                    XWPFParagraph optionPara = document.createParagraph();
                    XWPFRun optionRun = optionPara.createRun();
                    if (option.isChecked()) {
                        CTShd cTShd = optionRun.getCTR().addNewRPr().addNewShd();
                        cTShd.setVal(STShd.CLEAR);
                        cTShd.setColor("auto");
                        cTShd.setFill("7CB5EC");
                        optionRun.setText("☑");
                    } else {
                        optionRun.setText("□");
                    }
                    optionRun.setText(String.join(".", option.getOptSymbol(), option.getOptContent()));
                }
            }
        }
        return document;
    }
}
